/*
* shrimp7 JS
* Copyright 2009 Rens Sparrius
*
* http://www.soepgarnaal.com/shrimp7/
* http://shrimp7.googlecode.com/
*
* Licensed under the BSD license.
*/

shrimp7.statusUpdate = shrimp7.fn.statusUpdate = {
	postError:NULL,
	twitterTooLong:NULL,
	twitterWasChecked:NULL,
	savedStatus:NULL,
	replyTo:{
		screenname:NULL,
		statusId:NULL
	},
	status:{
		type:NULL,
		message:NULL
	},
	starter:function(){
		var ThisContext = shrimp7.statusUpdate;
		
		if(config.system==='sidebar'){
			setDimensions(420,200,'width');
		}else{
			$("#screenHeaderTitle").append(' <span id="characterCounter"></span><span id="characterCounterSpecial"></span>');
		}
		
		$('#'+setting("lastTab")+"Checkbox",activeDom).attr(checked,checked);
		
		if(setting("lastTab")==='twitter'){
			twitterWasChecked=1
		}
		
		$("#datepicker",activeDom).datepicker({
			minDate:0,
			altField:'#behind',
			altFormat:'yy,mm,dd'
		});
		
		var today=new Date();
		var h=today.getHours();
		var m=today.getMinutes();
		
		if(h<10){
			h="0"+h;
		}
		if(m<10){
			m="0"+m;
		}
		
		$("#timepicker",activeDom).val(h+':'+m);
		
		/*
		* When there is a saved status, fill the textarea
		*/
		if(ThisContext.savedStatus!==NULL){
			$("#statusTextarea",activeDom).val(ThisContext.savedStatus);
		}
		
		if(setting("boolTwitter")!==true){
			$("#postToTwitter",activeDom).hide();
		}else{
			if(ThisContext.replyTo.screenname!==NULL){
				$("#statusTextarea",activeDom).val('@'+ThisContext.replyTo.screenname+' ');
			}
		}
		if(setting("boolFacebook")!==true){
			$("#postToFacebook",activeDom).hide();
		}else{
			if(shrimp7.facebook.permission.publish_stream===UNDEFINED||!shrimp7.facebook.permission.publish_stream){
				shrimp7.facebook.con.hasAppStatusPermission("publish_stream",randomString(),function(response){
					if(response===0){
						$("#statusTextarea",activeDom).hide();
						$("#statusEditForm",activeDom).html('shrimp7 has no permission to change your Facebook status. <a href="http://www.facebook.com/authorize.php?api_key=460e6842edb265d791b35f4a3c5a0b4d&v=1.0&ext_perm=publish_stream"target="_blank">Click here to change</a> the permissions');
					}
				});
			}
			else{
				if(!shrimp7.facebook.permission.read_stream){
					$("#statusTextarea",activeDom).hide();
					$("#statusEditForm",activeDom).html('shrimp7 has no permission to change your Facebook status. <a href="http://www.facebook.com/authorize.php?api_key=460e6842edb265d791b35f4a3c5a0b4d&v=1.0&ext_perm=publish_stream">Click here to change</a> the permissions');
				}
			}
		}
		if(setting("boolFriendfeed")!==true){
			$("#postToFriendfeed",activeDom).hide();
		}
		
		if(ThisContext.status.message!==NULL&&ThisContext.status.type==='copy'){
			$("#flyoutTitle",activeDom).html('Copy');
			$("#setStatus",activeDom).val('Copy');
			$("#statusTextarea",activeDom).val(ThisContext.status.message);
		}
		
		/*
		* Init counter function..
		*/
		ThisContext.countChars();
		
		$("#twitterCheckbox",activeDom).unbind('click').click(function(){
			if($("#twitterCheckbox",activeDom).is(":checked")){
				ThisContext.twitterWasChecked = 1;
			}else{
				ThisContext.twitterWasChecked = NULL;
			}
		});
		
		return window.shrimp7;
	},
	close:function(){
		
	},
	/*
	* statusUpdate.setStatus
	*/
	setStatus:function(){
		var	THIS = this,
			message = $("#statusTextarea",activeDom).val(),
			twitterInReplyToId;
		
		/*
		* When no service is selected..
		*/
		if(!$("#twitterCheckbox",activeDom).is(":checked")&&!$("#facebookCheckbox",activeDom).is(":checked")&&!$("#friendfeedCheckbox",activeDom).is(":checked"))
			THIS.postError = 'Please select the service(s) you want to post to';
		
		if($("#statusTextarea",activeDom).val()==="")
			THIS.postError = 'You cannot post an empty message..';
		
		if($("input[name='when']:checked",activeDom).val()=='later'){
			var timeSplit = $("#timepicker").val().split(':'),
				dateSplit,
				myDate;
			if($("#behind",activeDom).val()=='')
				THIS.postError = 'Please click in the datefield to select a date';
			
			if(isNaN(timeSplit[0])||timeSplit[0]<0||timeSplit[0]>23)
				THIS.postError = 'Time must be in the HH:MM format';
			
			if(isNaN(timeSplit[1])||timeSplit[1]<0||timeSplit[1]>59)
				THIS.postError = 'Time must be in the HH:MM format';
			
			if(THIS.postError===NULL){
				myDate = new Date();
				dateSplit = $("#behind",activeDom).val().split(',');
				myDate.setFullYear(dateSplit[0],dateSplit[1]-1,dateSplit[2]);
				myDate.setHours(timeSplit[0],timeSplit[1]);
				if(myDate.getTime()!==NaN){
					if($("#twitterCheckbox",activeDom).is(":checked"))
						shrimp7.twitter.twitterQueue.push({timestamp:myDate.getTime(),message:message});
					
					if($("#facebookCheckbox",activeDom).is(":checked"))
						shrimp7.facebook.facebookQueue.push({timestamp:myDate.getTime(),message:message});
				}
				
				THIS.postError = 'You\'re message is queued.. don\'t close shrimp7!';
			}
		}
		
		if(THIS.postError===NULL){
			$("#postWarning",activeDom).html("");
			/*
			* Disable the post button..
			*/
			$("#postStatus",activeDom).attr({"disabled":"disabled","value":"Loading.."});
			/*
			* Twitter
			*/
			if($("#twitterCheckbox",activeDom).is(":checked")){
				$("#postStatusTwitter",activeDom).html(htmlImgIconsFront+'loading_dots.gif"alt=""/>');
				
				if(THIS.replyTo.statusId!==NULL)
					THIS.twitterInReplyToId = THIS.replyTo.statusId;
				else
					THIS.twitterInReplyToId = false;
				
				shrimp7.twitter.con.newTweet({
					tweet:message,
					inReplyTo:twitterInReplyToId,
					success:function(data){
						shrimp7.twitter.statusCounter++;
						$("#postStatusTwitter",activeDom).html(htmlImgIconsFront+'checked.png"/>');
						setTimeout(function(){shrimp7.twitter.fetchTimeline(true);},2000);
					},
					error:function(){
						$("#postStatusTwitter",activeDom).html(htmlImgIconsFront+'error.png"/>');
					}
				});
			}
			/*
			* Facebook
			*/
			if($("#facebookCheckbox",activeDom).is(":checked")){
				$("#postStatusFacebook",activeDom).html(htmlImgIconsFront+'loading_dots.gif"alt=""/>');
				shrimp7.facebook.con.writeStream(message,NULL,function(data){
					if(data!=='error')
						$("#postStatusFacebook",activeDom).html(htmlImgIconsFront+'checked.png"/>');
					else
						$("#postStatusFacebook",activeDom).html(htmlImgIconsFront+'error.png"/>');
				});
			}
			/*
			* Friendfeed
			*/
			if($("#friendfeedCheckbox",activeDom).is(":checked")){
				$("#postStatusFriendfeed",activeDom).html(htmlImgIconsFront+'loading_dots.gif"alt=""/>');
				oFriendfeed.publishToFeed(message,function(data){
					if(data!=='error'){
						$("#postStatusFriendfeed",activeDom).html(htmlImgIconsFront+'checked.png"/>');
					}else{
						$("#postStatusFriendfeed",activeDom).html(htmlImgIconsFront+'error.png"/>');
					}
				});
			}
		}else{
			$("#postWarning",activeDom).html(THIS.postError);
			THIS.postError = NULL;
		}
		return window.shrimp7;
	},
	/*
	* statusUpdate.countChars
	*/
	countChars:function(){
		var THIS = this,doit = function(){
			var totalChars = $('#statusTextarea',activeDom).val().length;
			
			if($("#twitterCheckbox",activeDom).is(":checked")&&totalChars>140){
				$("#twitterCheckbox",activeDom).attr("checked","");
				$("#postWarning",activeDom).html('Status too long for Twitter! Twitter disabled..');
				THIS.twitterTooLong = 1;
			}
			if(THIS.twitterTooLong===1&&THIS.twitterWasChecked===1&&THIS.totalChars<=140){
				$("#twitterCheckbox",activeDom).attr(checked,checked);
				$("#postWarning",activeDom).html('Twitter re-enabled');
			}
			
			$("#characterCounter",activeDom).html($("#statusTextarea",activeDom).val().length);
		}
		$("#statusTextarea",activeDom).keyup(function(){
			doit();
		});
		doit();
		return window.shrimp7;
	}
};